package com.xdtech.hotel.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xdtech.common.utils.JsonUtil;
import com.xdtech.core.dao.BaseDao;
import com.xdtech.core.model.BaseItem;
import com.xdtech.core.orm.Page;
import com.xdtech.core.orm.utils.BeanUtils;
import com.xdtech.hotel.dao.DeviceOperationDao;
import com.xdtech.hotel.model.DeviceOperation;
import com.xdtech.hotel.protocol.manager.RoomMonitor;
import com.xdtech.hotel.protocol.vo.Ac;
import com.xdtech.hotel.protocol.vo.Card;
import com.xdtech.hotel.protocol.vo.Dimmer;
import com.xdtech.hotel.protocol.vo.Music;
import com.xdtech.hotel.protocol.vo.Node;
import com.xdtech.hotel.protocol.vo.Scene;
import com.xdtech.hotel.protocol.vo.Tv;
import com.xdtech.hotel.service.DeviceOperationService;
import com.xdtech.hotel.service.DeviceService;
import com.xdtech.hotel.service.RoomService;
import com.xdtech.hotel.util.HotelConstants;
import com.xdtech.hotel.vo.DeviceOperationItem;
import com.xdtech.hotel.vo.RoomItem;
import com.xdtech.web.model.Pagination;

/**
 * 
 * @author max.zheng
 * @create 2015-04-23 22:10:38
 * @since 1.0
 * @see
 */
@Service
public class DeviceOperationServiceImpl implements DeviceOperationService {
	@Autowired
	private DeviceOperationDao deviceOperationDao;
	@Autowired
	private BaseDao baseDao;
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param entity
	 */
	public void save(DeviceOperation entity) {
		deviceOperationDao.save(entity);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param entity
	 */
	public void saveAll(List<DeviceOperation> entities) {
		deviceOperationDao.saveAll(entities);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param entity
	 */
	public void delete(DeviceOperation entity) {
		deviceOperationDao.delete(entity);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param id
	 */
	public void delete(Long id) {
		deviceOperationDao.delete(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param id
	 * @return
	 */
	public DeviceOperation get(Long id) {
		return deviceOperationDao.get(id);
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @return
	 */
	public List<DeviceOperation> getAll() {
		return deviceOperationDao.getAll();
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param pg
	 * @param values
	 * @return
	 */
	public Map<String, Object> loadPageAndCondition(Pagination pg,
			Map<String, String> values) {
		Map<String, Object> results = new HashMap<String, Object>();
		List<Object> deviceOperations = null;
		long rows = 0;
		if (pg!=null) {
			Page<DeviceOperation> page = deviceOperationDao.findPage(pg,"from DeviceOperation order by createTime desc", values);
			deviceOperations = BeanUtils.copyListProperties(
					DeviceOperationItem.class, page.getResult());
			rows = page.getTotalItems();
		}else {
			List<DeviceOperation> deviceOperationList = deviceOperationDao.find("from DeviceOperation order by id desc", values);
			deviceOperations = BeanUtils.copyListProperties(
					DeviceOperationItem.class, deviceOperationList);
			rows = deviceOperations.size();
		}
		results.put("total", rows);
		results.put("rows", deviceOperations);
		return results;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param item
	 * @return
	 */
	public boolean saveOrUpdateDeviceOperation(DeviceOperationItem item) {
		DeviceOperation deviceOperation = null;
		if (item.getId()==null) {
			deviceOperation = new DeviceOperation();
		}else {
			deviceOperation = deviceOperationDao.get(item.getId());
		}
		//复制前台修改的属性
		BeanUtils.copyProperties(deviceOperation, item);
		deviceOperationDao.save(deviceOperation);
		return true;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param deviceOperationId
	 * @return
	 */
	public DeviceOperationItem loadDeviceOperationItem(Long deviceOperationId) {
		DeviceOperation deviceOperation = deviceOperationDao.get(deviceOperationId);
		DeviceOperationItem deviceOperationItem = new DeviceOperationItem();
		BeanUtils.copyProperties(deviceOperationItem, deviceOperation);
		return deviceOperationItem;
	}

	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param id
	 * @return
	 */
	public boolean deleteDeviceOperationInfo(long id) {
		delete(id);
		return true;
	}
	
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @param newIds
	 * @return
	 */
	public boolean deleteDeviceOperationInfo(List<Long> deviceOperationIds) {
		for (Long id : deviceOperationIds) {
			delete(id);
		}
		return true;
	}
	
	public Map<String, Object> loadPageDataByConditions(Pagination pg,
			BaseItem baseItem, String queryName) {
		Map<String, Object> results = new HashMap<String, Object>();
		Page page = baseDao.findPageByNamedQuery(pg, queryName,baseItem);
		results.put("total", page.getTotalItems());
		results.put("rows", page.getResult());
		return results;
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-04-23 22:10:38
	 * @modified by
	 * @return
	 */
	@Override
	public List<DeviceOperationItem> loadItems() {
		List<DeviceOperation> deviceOperations = getAll();
		return DeviceOperationItem.createItems(deviceOperations);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-25下午10:14:07
	 * @modified by
	 * @param selectOperations
	 * @return
	 */
	@Override
	public List<DeviceOperation> loadDeviceOperations(
			List<Long> selectOperations) {
		return deviceOperationDao.get(selectOperations);
	}
	/**
	 * @description
	 * @author max.zheng
	 * @create 2015-4-25下午10:32:51
	 * @modified by
	 * @param linkageId
	 * @return
	 */
	@Override
	public Map<String, Object> loadListByLinkageId(Long linkageId) {
		List<DeviceOperation> deviceOperations = getAll();
		
		String sHql = "select do from DeviceOperation do,LinkageConfig lc where do.id in elements (lc.deviceOperations) and lc.id=?";
		List<DeviceOperation> selectedDeviceOperations = deviceOperationDao.findByHql(sHql, linkageId);
		Map<Long, DeviceOperation> selectedMap = new HashMap<Long, DeviceOperation>();
		for (DeviceOperation deviceOperation : selectedDeviceOperations) {
			selectedMap.put(deviceOperation.getId(), deviceOperation);
		}
		List<DeviceOperationItem> items = new ArrayList<DeviceOperationItem>();
		DeviceOperationItem item = null;
		for (DeviceOperation deviceOperation : deviceOperations) {
			item = DeviceOperationItem.createItem(deviceOperation);
			if (selectedMap.containsKey(deviceOperation.getId())) {
				item.setSelected(true);
			}else {
				item.setSelected(false);
			}
			items.add(item);
		}
		Map<String, Object> results = new HashMap<String, Object>();
		results.put("total", items.size());
		results.put("rows", items);
		results.put("selects", selectedMap.keySet());
		return results;
	}
	
}
